@extends('admin.layouts.admin')
@section('header')
    <link rel="stylesheet" href="{{ asset('admin/style/admin.css') }}" media="all"> @endsection
@section('content')
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <div class="layui-input-inline lay-search">
                            <input type="text" name="name" placeholder="名称" autocomplete="off" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        @include('admin.layouts._layout.form_search')
                    </div>
                </div>
            </div>

            <div class="layui-card-body">
                <div style="padding-bottom: 10px;">
                    @include('admin.layouts._layout.form_button_top')
                    <button class="layui-btn layui-btn-normal layuiadmin-btn-search" data-type="rest_cost">重置最新价格
                    </button>
                    <button class="layui-btn layui-btn-normal layuiadmin-btn-search" data-type="init_stock_list">重置股票池
                    </button>
                </div>

                <table id="LAY-form" lay-filter="LAY-form"></table>

                <script type="text/html" id="tpl_name">
                    <span>@{{d.html}} @{{d.name}}</span>
                </script>
                <script type="text/html" id="checkboxTpl">
                    <!-- 这里的 checked 的状态只是演示 -->
                    <input type="checkbox" name="attention" value="@{{d.id}}" title="锁定" lay-filter="lockDemo" @{{
                           d.attention== 1 ? 'checked' : '' }}>
                </script>
                <script type="text/html" id="warmTpl">
                    <input type="checkbox" name="warm" value="@{{d.id}}" title="是" lay-filter="lockWarm" @{{ d.warm== 1
                           ? 'checked' : '' }}>
                </script>

                <script type="text/html" id="initTpl">
                    @{{#  if(d.is_init == 2){ }}
                    <a class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="isInit"><i class="layui-icon layui-icon-edit"></i>同步</a>
                    @{{#  } else { }}
                    <a class="layui-btn layui-btn-disabled layui-btn-xs"></i>已同步</a>
                    @{{#  } }}
                </script>

                <script type="text/html" id="finaTpl">
                    @{{#  if(d.is_fina == 2){ }}
                    <a class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="isFina"><i class="layui-icon layui-icon-edit"></i>同步</a>
                    @{{#  } else { }}
                    <a class="layui-btn layui-btn-disabled layui-btn-xs"></i>已同步</a>
                    @{{#  } }}
                </script>

                <script type="text/html" id="realTpl">
                    @{{#  if(d.is_real == 2){ }}
                    <a class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="isReal"><i class="layui-icon layui-icon-edit"></i>同步</a>
                    @{{#  } else { }}
                    <a class="layui-btn layui-btn-disabled layui-btn-xs"></i>已同步</a>
                    @{{#  } }}
                </script>

                <script type="text/html" id="table-controller">
                    @include('admin.layouts._layout.form_button')
                </script>
            </div>
        </div>
    </div>
    <form id="destroy_form" method="post">{{ csrf_field() }} {{ method_field('DELETE') }}</form>
@endsection
@section('scripts')
    <script>
        layui.config({
            base: '../' //静态资源所在路径
        }).use(['table', 'form'], function () {
            var $ = layui.$
                , form = layui.form
                , table = layui.table
                , views = layui.view;
            var _actions = 'stock';

            /**
             * 表格列表
             */
            var cols = [[
                {type: "checkbox", fixed: "left"},
                {type: "numbers", title: "ID"}
                , {field: "name", title: "名称"}
                , {field: "code", title: "编码"}
                , {field: "cost", title: "当前价格"}
                , {field: "add_cost", title: "加入时价格"}
                , {field: 'attention', title: '是否置顶', width: 110, templet: '#checkboxTpl', unresize: true}
                , {field: 'warm', title: '是否提醒', width: 110, templet: '#warmTpl', unresize: true}
                // ,{field: "order", title: "排序", edit: 'text'}
                , {field: "created_at", title: "创建时间", sort: !0}
                , {field: "updated_at", title: "修改时间", sort: !0}
                , {field: "is_init", title: "历史数据", templet: '#initTpl'}
                // , {field: "is_fina", title: "财务数据", templet: '#finaTpl'}
                // , {field: "is_real", title: "实时提示", templet: '#realTpl'}
                , {title: '操作', minWidth: 150, align: 'center', fixed: 'right', toolbar: '#table-controller'}]];

            var initTable = Common.initTable(cols);

            /**
             * 监听搜索
             */
            form.on('submit(LAY-search)', function (data) {
                Common.search(data, 1);
            });

            /**
             * 监听重置
             */
            form.on('submit(LAY-reset)', function (data) {
                $('.lay-search input').val('');
                $(".layui-form select").find("option:first").prop("selected", true);
                form.render();
                Common.search(data, 2);
            });

            /**
             * 监听锁定操作
             */
            form.on('checkbox(lockDemo)', function (obj) {
                var url = "/admin/" + _actions + "/set_attention?id=" + this.value;
                Common.setStatus(url)
            });

            form.on('checkbox(lockWarm)', function (obj) {
                var url = "/admin/" + _actions + "/set_warm?id=" + this.value;
                Common.setStatus(url)
            });

            /**
             * 监听排序
             */
            table.on('sort(LAY-form)', function (obj) {
                Common.sort(obj);
            });

            /**
             * 监听单元格编辑
             **/
            table.on('edit(LAY-form)', function (obj) {
                var postUrl = "/admin/" + _actions + "/update/" + obj.data.id
                Common.editCols(postUrl, obj);
            });

            /**
             * 操作监听 删除和修改
             * @controllers del edit
             */
            table.on("tool(LAY-form)", function (e) {
                if ("del" === e.event) {
                    var postUrl = "/admin/" + _actions + "/destroy";
                    Common.deleteTable(postUrl, e);
                } else if ("edit" === e.event) {
                    var type = 2;
                    var title = '编辑';
                    var modalUrl = "/admin/" + _actions + "/edit/" + e.data.id;
                    var postUrl = "/admin/" + _actions + "/update/" + e.data.id;
                    var width = '500px';
                    var height = '400px';
                    Common.editTable(type, title, modalUrl, postUrl, width, height);
                } else if ("isInit" === e.event) {
                    var index = layer.load(2, {
                        shade: [0.1,'#fff'], //0.1透明度的白色背景
                        time: 10*1000
                    });

                    var url = "/admin/" + _actions + "/is_init?id=" + e.data.id;
                    Common.setStatus(url)


                    layer.close(index);

                }
            });

            /**
             * 事件 添加和批量删除
             * @controllers {batchdel: batchdel, add: add}
             */
            $('.layui-btn.layuiadmin-btn-search').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
            var active = {
                batchdel: function () {
                    var postUrl = "/admin/" + _actions + "/destroy";
                    Common.batchDeleteTable(postUrl);
                }
                , add: function () {
                    var type = 2;
                    var title = '添加';
                    var modalUrl = "/admin/" + _actions + "/create";
                    var postUrl = "/admin/" + _actions + "/store";
                    var width = '500px';
                    var height = '400px';
                    Common.addTable(title, modalUrl, postUrl, width, height);
                }
                , rest_cost: function () {
                    layer.msg('更新价格数据<br>', {
                        time: 30000, //20s后自动关闭
                        btn: ['更新当前价格', '更新加入价格', '关闭']
                        , yes: function () {
                            $.ajax({
                                type: "get",
                                url: "{{ route('admin.stock.rest_cost') }}",
                                data: '',
                                error: function () {
                                    Common.systemReturn('系统错误');
                                },
                                success: function (data) {
                                    if (data.code == 0) {
                                        Common.successReturn(data)
                                    } else {
                                        Common.errorReturn(data)
                                    }
                                }
                            });
                        }
                        , btn2: function () {
                            $.ajax({
                                type: "get",
                                url: "{{ route('admin.stock.rest_cost') }}" + "?type=1",
                                data: '',
                                error: function () {
                                    Common.systemReturn('系统错误');
                                },
                                success: function (data) {
                                    if (data.code == 0) {
                                        Common.successReturn(data)
                                    } else {
                                        Common.errorReturn(data)
                                    }
                                }
                            });
                        }
                        , btn3: function () {
                            alert('1')
                        }
                    });


                }, init_stock_list: function () {
                    layer.msg('重置中...', {
                        icon: 16
                        ,shade: 0.01
                    }, function(){
                        $.ajax({
                            type: "get",
                            url: "{{ route('admin.stock.init_stock_list') }}",
                            data: '',
                            error: function () {
                                Common.systemReturn('系统错误');
                            },
                            success: function (data) {
                                if (data.code == 0) {
                                    Common.successReturn(data)
                                } else {
                                    Common.errorReturn(data)
                                }
                            }
                        });
                        layer.close(index);
                    });


                }
            };
        });//use

    </script>
@stop
